package controllers

import java.io.File
import javax.inject.Inject
import play.api.mvc.{AbstractController, ControllerComponents}
import tool.FormTool
import utils.Utils

import scala.concurrent.ExecutionContext.Implicits.global
import implicits.Implicits._
import play.api.libs.json.Json


/**
 * Created by Administrator on 2019/8/8
 */
class ToolController @Inject()(cc: ControllerComponents, formTool: FormTool) extends AbstractController(cc) {

  def downloadExampleData = Action {
    implicit request =>
      val data = formTool.fileNameForm.bindFromRequest().get
      val exampleDir = Utils.exampleFile
      val resultFile = new File(exampleDir, data.fileName)
      Ok.sendFile(resultFile).withHeaders(
        CONTENT_DISPOSITION -> s"attachment; filename=${
          resultFile.getName
        }",
        CONTENT_TYPE -> "application/x-download"
      )
  }

  def getTestPlatform = Action {
    implicit request =>
      val file = new File(Utils.dataDir, "test_platform.txt")
      val map = file.txtLines.map { columns =>
        (columns.head, columns.last)
      }.toSeqMap
      Ok(Json.toJson(map))
  }

}
